﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage4 : Stage
    {
        public Stage4(Game game) : base(game) { }
        public override void InitializeScenes()
        {
            base.InitializeScenes();

            Animation title = new Animation("images/title/stageTitle", new Rectangle(158, 190, 88, 34), new Point(88, 34));
            title.Scale = 1.2f;

            Transition tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(383 + 150, 377 - 200), 0, 1, 80, 220);//460, 391
            tra.Register(this);

            for (int i = 1; i < 2; i++)
            {
                Animation a = new Animation("images/stages/stage4/stageLab_" + i);
                a.Scale = 1.5f;
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }
            _backGroundPos = new Vector2(-100, -500);
            _scenceBound = new Rectangle(0, -500,
                  1000,1000);
            camera.Bound = _scenceBound;
            camera.MoveSpeed = 5f;

            for (int i = 0; i < 2; i++)
            {
                Animation wallBack = new Animation("images/stages/stage4/stage4_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-420, -853);

            for (int i = 2; i < 4; i++)
            {
                Animation wallBack = new Animation("images/stages/stage4/stage4_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGroundOther.Add(wallBack);
            }
            _wallBackGroungOtherPos = new Vector2(-420, 171);


            GroundObject grount;
            ElementPiece element;
            GameDoor door;
            GameKey key;
            Switch switch_1;
            ProtalGate protalSwitch;
            Mechine mechine;
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(7, -52); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(400, 1062)); grount.Alpha = 0f; grount.Size = new Vector2(400, 1062); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(717, 453); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(1024, 51)); grount.Alpha = 0f; grount.Size = new Vector2(1024, 51); grount.Rotation = 0f; Actors.Add(grount);
            protalSwitch = new ProtalGate("Stage4_Protal_1", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage3", "Stage3_Protal_2"); protalSwitch.Position = new Vector2(383, 377); Actors.Add(protalSwitch);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(776, 374); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(216, 110)); grount.Alpha = 0f; grount.Size = new Vector2(216, 110); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(541, 371); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(83, 10)); grount.Alpha = 0f; grount.Size = new Vector2(83, 10); grount.Rotation = 0f; Actors.Add(grount);
            element = new ElementPiece("Stage4_ele_1", this._world, this); element.Position = new Vector2(258, 412); Actors.Add(element);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1429, -72); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(400, 1024)); grount.Alpha = 0f; grount.Size = new Vector2(400, 1024); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(-198, -639); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(37, 378)); grount.Alpha = 0f; grount.Size = new Vector2(37, 378); grount.Rotation = 0f; Actors.Add(grount);
            protalSwitch = new ProtalGate("Stage4_protal_2", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("stage5", "stageSnowProtal0"); protalSwitch.Position = new Vector2(-38, -634); Actors.Add(protalSwitch);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(837, 52); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(680, 70)); grount.Alpha = 0f; grount.Size = new Vector2(680, 70); grount.Rotation = 0f; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1127, 327); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(206, 204)); grount.Alpha = 0f; grount.Size = new Vector2(206, 204); grount.Rotation = 0f; Actors.Add(grount);

            //第一個上升地板
            grount = new MechanismGound(this._world, this); grount.Position = new Vector2(954, 373);
            grount.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(2,2,138,109),new Point(138, 109)); 
            grount.Size = new Vector2(138, 109); grount.Rotation = 0f; Actors.Add(grount);
            
            switch_1 = new SensorSwitch(_world, this); switch_1.AddTarget(grount); switch_1.Position = new Vector2(733, 305); switch_1.Size = new Vector2(50, 50); Actors.Add(switch_1);

            ModifierContain up = new ModifierContain(grount);
            ModifierContain down = new ModifierContain(grount);
            //設定機關行為
            PositionModifier m1 = new PositionModifier(grount.Position, grount.Position + new Vector2(0, -50), 0, 15);
            PositionModifier m4 = new PositionModifier(grount.Position + new Vector2(0, -50), grount.Position, 0, 15);
            up.AddModifier(m1);
            down.AddModifier(m4);

            MechanismGound me = (MechanismGound)grount;
            me.PositiveAction.Add(up);
            me.NegetiveAction.Add(down);
            //me.PositiveAction = up;
            //me.NegetiveAction = down;
            switch_1 = new Switch(_world, this);

            
            //最右邊的樓梯
            grount = new Floor(this._world, this); 
            grount.Position = new Vector2(1170, -110); 
            grount.CurrentAnimation = new Animation("images/items/floor", new Point(40, 190)); 
            grount.Size = new Vector2(40, 190); grount.Rotation = 0f; Actors.Add(grount);
            
            switch_1.AddTarget(grount);
            switch_1.Position = new Vector2(824, 293);
            switch_1.Size = new Vector2(50, 50);
            up = new ModifierContain(grount);
            down = new ModifierContain(grount);
            //設定機關行為
            PositionModifier m2 = new PositionModifier(grount.Position, grount.Position + new Vector2(0, 190), 0, 60);
            PositionModifier m3 = new PositionModifier(grount.Position + new Vector2(0, 190), grount.Position, 0, 60);
            down.AddModifier(m2);
            up.AddModifier(m3);

            me = (MechanismGound)grount;
            me.PositiveAction.Add(down);
            me.NegetiveAction.Add(up);

            Actors.Add(switch_1);

            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1157, -179); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(142, 26)); grount.Alpha = 0f; grount.Size = new Vector2(142, 26); grount.Rotation = 0f; Actors.Add(grount);
            element = new ElementPiece("Stage4_ele_2", this._world, this); element.Position = new Vector2(982, -283); Actors.Add(element);
            
            SensorSwitch switch_0 = new SensorSwitch(_world, this);
            switch_0.Position = new Vector2(740, 3);
            switch_0.Size = new Vector2(50, 50);
            
            //左邊下面往左右移動的牆
            grount = new MechanismGound(this._world, this); grount.Position = new Vector2(247, -36);
            grount.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(142, 90, 80, 14), new Point(80, 14));  
            grount.Size = new Vector2(80, 14); grount.Rotation = 0f; Actors.Add(grount);
            Actors.Add(switch_0);
            switch_0.AddTarget(grount);

            ModifierContain right = new ModifierContain(grount);
            ModifierContain left = new ModifierContain(grount);
            //設定機關行為
            m2 = new PositionModifier(grount.Position, grount.Position + new Vector2(150, 0), 0, 30);
            m3 = new PositionModifier(grount.Position + new Vector2(150, 0), grount.Position, 0, 30);
            right.AddModifier(m2);
            left.AddModifier(m3);

            me = (MechanismGound)grount;
            me.PositiveAction.Add(right);// = right;
            me.NegetiveAction.Add(left);// = left;

            switch_0 = new SensorSwitch(_world, this);
            switch_0.Position = new Vector2(640, 3);
            switch_0.Size = new Vector2(50, 50);

            //左邊上面的牆
            grount = new MechanismGound(this._world, this); grount.Position = new Vector2(247, -91);
            grount.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(142, 90, 80, 14), new Point(80, 14)); 
            grount.Size = new Vector2(80, 14); grount.Rotation = 0f; Actors.Add(grount);
            Actors.Add(switch_0);
            switch_0.AddTarget(grount);

            right = new ModifierContain(grount);
            left = new ModifierContain(grount);
            //設定機關行為
            m2 = new PositionModifier(grount.Position, grount.Position + new Vector2(75, 0), 0, 20);
            m3 = new PositionModifier(grount.Position + new Vector2(75, 0), grount.Position, 0, 20);
            right.AddModifier(m2);
            left.AddModifier(m3);

            me = (MechanismGound)grount;
            me.PositiveAction.Add(right);// = right;
            me.NegetiveAction.Add(left);// = left;

            //左邊最長的樓梯
            grount = new Floor(this._world, this); grount.Position = new Vector2(517, -110); grount.CurrentAnimation = new Animation("images/items/floor", new Point(40, 330)); grount.Size = new Vector2(40, 330); grount.Rotation = 0f; Actors.Add(grount);

            switch_1 = new Switch(_world, this); switch_1.AddTarget(grount); switch_1.Position = new Vector2(900, -9); switch_1.Size = new Vector2(50, 50); Actors.Add(switch_1);

            up = new ModifierContain(grount);
            down = new ModifierContain(grount);
            //設定機關行為
            m2 = new PositionModifier(grount.Position, grount.Position + new Vector2(0, 270), 0, 75);
            m3 = new PositionModifier(grount.Position + new Vector2(0, 270), grount.Position, 0, 75);
            down.AddModifier(m2);
            up.AddModifier(m3);

            me = (MechanismGound)grount;
            me.PositiveAction.Add(down);// = down;
            me.NegetiveAction.Add(up);// = up;

            
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(247, -147);
            grount.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(142, 90, 80, 14), new Point(80, 20));  
            grount.Size = new Vector2(80, 20); grount.Rotation = 0f; Actors.Add(grount);
           
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(547, -324); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(682, 12)); grount.Alpha = 0f; grount.Size = new Vector2(682, 12); grount.Rotation = 0f; Actors.Add(grount);
            
            door = new GameDoor("Stage4_Door_S1", _world, this); door.Position = new Vector2(776, -380); door.Size = new Vector2(60, 100); door.DoorID = "Stage4_Door_1"; Actors.Add(door);
            key = new GameKey("Stage4_Key_S1", _world, this); key.Position = new Vector2(239, -174); key.Size = new Vector2(23, 35); key.KeyID = "Stage4_Door_1"; Actors.Add(key);
            //grount = new GroundObject(this._world, this); grount.Position = new Vector2(547, -364); grount.CurrentAnimation = new Animation("images/system/black", new Point(84, 70)); grount.Size = new Vector2(84, 70); grount.Rotation = 0f; Actors.Add(grount);
            mechine = new Mechine("Stage4_Mechine_1", _world, this);
            mechine.Position = new Vector2(658, -369); Actors.Add(mechine);
            mechine.Capacity = 2;

            MechanismGound mechanismGound =
            StateFactory.CreateMechanismGound(this, 57, -351, 300, 42, 0, 300, 0, 0, 75); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(142, 2, 300, 42), new Point(300, 42));

            mechanismGound=
            StateFactory.CreateMechanismGound(this, 87, -393, 240, 42, 0, 240, 0, 0, 60); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(2, 113, 240, 42), new Point(240, 42));

            mechanismGound =
            StateFactory.CreateMechanismGound(this, 117, -435, 180, 42, 0, 180, 0, 0, 45); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(142, 46, 180, 42), new Point(180, 42));

            mechanismGound =
            StateFactory.CreateMechanismGound(this, 147, -477, 120, 42, 0, 120, 0, 0, 30); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(2, 157, 120, 42), new Point(120, 42));

            mechanismGound =
            StateFactory.CreateMechanismGound(this, 177, -519, 60, 42, 0, 60, 0, 0, 15); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/ground/stage4wall", new Rectangle(444, 2, 60, 42), new Point(60, 42));

            grount = new GroundObject(this._world, this); grount.Position = new Vector2(984, -254); grount.CurrentAnimation = new Animation("images/ground/labwall", new Point(100, 12)); grount.Alpha = 0; grount.Size = new Vector2(100, 12); grount.Rotation = 0f; Actors.Add(grount);

            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f,false);
            //MusicManager.AddMusic("Music/hurry", "first", 0.001f,true);

            _song = MusicManager.LoadSong("Music/stage4");
            GameClass._musicString = "Music/stage4";
            MediaPlayer.Play(_song);
            if (MediaPlayer.Volume>=0.5f)
            MediaPlayer.Volume = 0.5f;
            MediaPlayer.IsRepeating = true;

            _startProtal = new ProtalCommand("stage4", "Stage4_Protal_1");

        }

        public void asg()
        {
            camera.Scale -= 0.002f;
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            base.OnDraw(spriteBatch, delta);
            //GraphicsDevice.Clear(Color.White);


            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();
            DrawWalls(spriteBatch);
            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "" + test, new Vector2(50, 50), Color.Black);
            //spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);

            //a.Update(delta);
            // MusicManager.FadeInMusic("first");


        }

        public override Stage Clone()
        {
            Stage4 stage = new Stage4(Game);
            stage.InitializeScenes();
            return stage;
        }

        public override void Dispose()
        {
            base.Dispose();
            //mic.Dispose();
        }
    }
}
